package com.cakeclient;


import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.actionbarsherlock.app.SherlockFragment;
import com.cakeclient.Model.Basket;
import com.cakeclient.Model.Parameters;

public class ClientBasketFragment extends SherlockFragment {
    private Basket basket;
    private ListView listView;
    private Button client_basket_button_send;
    private TextView client_basket_facebook_name;
    private ImageView imageViewFacebook;
    private LinearLayout linearLayoutFacebook;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.client_basket_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        basket = ((MainActivity) getActivity()).basket;


        listView = (ListView) getActivity().findViewById(R.id.basket_listview);

        displayBasket(basket);

        client_basket_facebook_name = (TextView) getActivity().findViewById(R.id.client_basket_facebook_name);
        imageViewFacebook = (ImageView) getActivity().findViewById(R.id.client_basket_facebook_imageview);
        linearLayoutFacebook = (LinearLayout) getActivity().findViewById(R.id.client_basket_facebook_mode);
    }


    @Override
    public void onResume() {
        super.onResume();

        if (((MainActivity)getSherlockActivity()).mFacebook.isSessionValid()) {
            linearLayoutFacebook.setVisibility(View.VISIBLE);
            client_basket_facebook_name.setText(Parameters.getFacebookName(getSherlockActivity()));
            Bitmap avatar = ((MainActivity)getSherlockActivity()).facebookImage;
            if (avatar != null){
                imageViewFacebook.setBackgroundDrawable(new BitmapDrawable(avatar));
            }
        } else {
            linearLayoutFacebook.setVisibility(View.GONE);
            client_basket_facebook_name.setText("Not Connected");

        }//To change body of overridden methods use File | Settings | File Templates.


    }



    public void displayBasket(Basket basket) {
        ListAdapter adapter = new ListViewClientBasketAdapter(getActivity(), R.layout.client_listview_basket_item, basket.getProducts());
        listView.setAdapter(adapter);
    }
}
